//
//  JaneHomeSecondController.m
//  EPA
//
//  Created by lanou3g on 16/4/23.
//  Copyright © 2016年 张静. All rights reserved.
//

#import "JaneHomeSecondController.h"
#import "JaneNetWorkRequestManager.h"
#import "JaneCommentController.h"
#import "DBManager.h"

@interface JaneHomeSecondController ()<UIWebViewDelegate>
@property (nonatomic,strong) NSString *htmlStr;
@property (nonatomic,strong) UIWebView *webView;
@end

@implementation JaneHomeSecondController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadFromContentID:nil];
    self.navigationItem.title = @"阅读详情";
    NSString *collectionTitle = nil;
    if ([[DBManager shareManager]hasCollect:(NSString *)self.homeListModel.ID]) {
        collectionTitle = @"取消收藏";
    }else {
        collectionTitle = @"收藏";
    }
    UIBarButtonItem *collectionBtn = [[UIBarButtonItem alloc]initWithTitle:collectionTitle style:UIBarButtonItemStylePlain target:self action:@selector(collectionBtnAction:)];
    UIBarButtonItem *commentBtn = [[UIBarButtonItem alloc]initWithTitle:@"评论" style:UIBarButtonItemStylePlain target:self action:@selector(commentBtnAction:)];
    UIBarButtonItem *shareBtn = [[UIBarButtonItem alloc]initWithTitle:@"分享" style:UIBarButtonItemStylePlain target:self action:@selector(shareBtnAction:)];
    self.navigationItem.rightBarButtonItems = @[collectionBtn,commentBtn,shareBtn];

    
    // Do any additional setup after loading the view.
}
- (void)collectionBtnAction:(UIBarButtonItem *)item {
    if ([item.title isEqualToString:@"收藏"]) {
        [[DBManager shareManager] collectDetailModel:self.homeListModel html:self.htmlStr];
        [item setTitle:@"取消收藏"];
    }else{
        [item setTitle:@"收藏"];
        [[DBManager shareManager] cancelCollectionArticle:(NSString *)self.homeListModel.ID];
    }
}
- (void)commentBtnAction:(UIBarButtonItem *)item {
    JaneCommentController *commentVC = [[JaneCommentController alloc]init];
    commentVC.homeListModel = self.homeListModel;
    [self.navigationController pushViewController:commentVC animated:YES];
}
- (void)shareBtnAction:(UIBarButtonItem *)item {
  
}

- (void)loadFromContentID:(NSString *)contentid {
    [JaneNetWorkRequestManager requestURL:[NSURL URLWithString:READCONTENT_URL] RequestMethod:CK_POST Params:@{@"contentid":self.homeListModel.ID,@"client":@"1",@"deviceid":@"63A94D37-33F9-40FF-9EBB-481182338873",@"auth":@"Wc06FCrkoq1DCMVzGMTikDJxQ8bm3Mrm2NpT9qWjwzcWP23tBKQx1c4P0",@"version":@"3.0.2"} Result:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (data) {
            NSMutableDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSDictionary *dataDict = jsonDict[@"data"];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self createWebView];
                self.htmlStr = dataDict[@"html"];
                [self.webView loadHTMLString:self.htmlStr baseURL:nil];
            });
        }
    }];
}
- (void)createWebView {
    self.webView = [[UIWebView alloc]initWithFrame:self.view.bounds];
//    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.htmlStr]]];
    self.webView.delegate = self;
    [self.view addSubview:self.webView];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
